package sokoban;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class PaintImg extends JPanel implements MapList{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4496011240733270833L;
	private Image Box, Target, Player, Player_on_target, Box_on_Target,Wall,Empty;
	private Image[][] images;
	
	public PaintImg(){
				
			String urlPrefix = "resources/sokoban/icons/";		
			String urlSuffix = "16x16.png";
			Empty = initializeImage(urlPrefix+"blank"+urlSuffix, 0, 0);
			Target=initializeImage(urlPrefix+"target"+urlSuffix, 0, 0);
			Box=initializeImage(urlPrefix+"movable"+urlSuffix, 0, 0);
			Wall=initializeImage(urlPrefix+"wall"+urlSuffix, 0, 0);
			Player=initializeImage(urlPrefix+"mover"+urlSuffix, 0, 0);
			Player_on_target=initializeImage(urlPrefix+"mover_on_target"+urlSuffix, 0, 0);
			Box_on_Target=initializeImage(urlPrefix+"movable_on_target"+urlSuffix, 0, 0);		
		
	}

	public void makeMapGrid(Grid map) {
			
			images = new Image[map.getGrid().length][map.getGrid()[0].length];			
			for(int i=0;i<map.getGrid().length;i++){
				for(int j=0;j<map.getGrid()[i].length;j++){
					Image curr=null;
					switch(map.getGrid()[i][j]){
						case CHARS.EMPTY:
							curr=Empty;
							break;
					
						case CHARS.BOX:
							curr=Box;
							break;
						case CHARS.BOX_ON_TARGET:
							curr=Box_on_Target;
							break;
						case CHARS.PLAYER:
							curr=Player;
							break;
						case CHARS.PLAYER_ON_TARGET:
							curr=Player_on_target;
							break;
						case CHARS.TARGET: 
							curr=Target;
							break;
						case CHARS.WALL:
							curr=Wall;						
							break;										
					}			
					images[i][j]=curr;			
				}
			}	
	}
	public Image initializeImage(String url, int x, int y){		
		try {
			Image tmg = (ImageIO.read(new File(url)));

			return tmg;
			//getPaintImg().drawImage(tmg);
		} catch (IOException e) {
			System.out.println("File not found");

			return null;
		}			

	}
	@Override
	public void paint(Graphics g) {
		if(images!=null){
		int y=0;//(int)(400-16*images.length)/2-30;
		int x=5;//(int)((350-(16*images[0].length))/2)-5;
		int qq=x;
			for(int i=0;i<images.length;i++){
				for(int j=0;j<images[i].length;j++){
					if(images[i][j]!=null){
					g.drawImage(images[i][j],x,y,this);
					}
					x+=16;
					
				}
				y+=16;
				x=qq;
			}
		}
		
	}
	
	@Override
	public void updateImages(char c, int x, int y){
		if(getImage(c)!=null){
			images[y][x]=getImage(c);
		}
	}
	private Image getImage(char c){
		switch(c){
			case CHARS.EMPTY:
				return Empty;
			case CHARS.BOX:
				return Box;
			case CHARS.TARGET:
				return Target;
			case CHARS.WALL:
				return Wall;
			case CHARS.PLAYER:
				return Player;
			case CHARS.PLAYER_ON_TARGET:
				return Player_on_target;
			case CHARS.BOX_ON_TARGET:
				return Box_on_Target;
			default: 
				return null;						
		}		
	}
	
	
	
}
